package leetcode_1001_1100;

import com.sun.org.apache.bcel.internal.generic.NOP;
import node.TreeNode;

public class LeeCode_1080 {
    public static void main(String[] args) {
        TreeNode treeNode = sufficientSubset(new TreeNode(4, new TreeNode(5), new TreeNode(3)), 8);
        System.out.println(treeNode);
    }
    private static TreeNode sufficientSubset(TreeNode root, int limit) {
        if (f(root, limit, 0)) {
            return null;
        }
        return root;
    }
    private static boolean f(TreeNode node, int limit, int sum){
        if (node == null)
            return true;
        if (node.left == null && node.right == null){
            return limit > sum + node.val;
        }
        boolean left = f(node.left, limit, sum + node.val);
        boolean right = f(node.right, limit, sum + node.val);
        if (left){
            node.left = null;
        }
        if (right){
            node.right = null;
        }
        return left && right;
    }
}
